package main;

import java.util.*;

/**
 *一组单词，对变位词进行分组，假设只包含英文小写字母
 * 质数代表字母，乘积唯一，作为key mn
 * 排序字符串唯一，作为key  nmlogm
 */
public class GroupAnagrams {
    public static void main(String[] args) {
        String[] strArr = new String[]{"eat","tea","tan","ate","nat","bat"};
        System.out.println(new GroupAnagrams().groupAnagrams(strArr));
    }

    public List<List<String>> groupAnagrams(String[] strs){
        Map<String,List<String>> groups = new HashMap<>();
        for (String str : strs){
            char[] sorted = str.toCharArray();
            Arrays.sort(sorted);
            String strSorted = new String(sorted);
            groups.putIfAbsent(strSorted,new LinkedList<>());
            groups.get(strSorted).add(str);
        }
        return new LinkedList<>(groups.values());
    }

}
